// 包含n个用来创建action的工厂函数（action creator）
import { setUser,setRolesButtons,removeUser,removeOrgInfo } from '../utils/storageUtils';
import { setToken,deleteToken, } from '../utils/cookies';
import { message } from 'antd'; 
import {RECEIVE_USER,SHOW_ERROR_MSG,SHOW_ORG_INFO,RESET_USER,RECEIVE_CURVES_INTO_DAOS,DELECT_CURVE_IN_DAO,
  ADD_CURVE_IN_DAO,RECEIVE_SELECTED_CURVES,ADD_LINE_DAO,ADD_DEPTH_DAO,ADD_YX_DAO,DELETE_DAO,EDIT_DEPTH_DATA,
  RECEIVE_DAOS_MB,EDIT_CURVE_INFO,RECEIVE_DOC,RECEIVE_MB,EDIT_DOC_INFO,ADD_YX_DATA,
  ADD_NEW_CURVE_IN_DAO,
  INIT_DAOSDATALIST,INIT_MBDATA,INIT_DOCDATA,RECEIVE_DEFAULT_MB,REPLACE_NEW_CURVES,UPDATE_DRAGGABLE,GET_CURVES_COUNT,CANCEL_SELECTED,RETURN_INIT,
  IS_UPDATE_PAGES,DRAG_CURVE_DATA,UPDATE_DAO_ORDER,SAVE_VIDEO_CLICK_YDATA,SAVE_VIDEO_DIRECTION,SAVE_VIDEO_MARKLINE,
  RECEIVE_DAOS_MB1,EDIT_DEPTH_DATA1,RECEIVE_CURVES_INTO_DAOS1,RECEIVE_SELECTED_CURVES1,RETURN_INIT1,EDIT_CURVE_INFO1,UPDATE_DAO_ORDER1,DELECT_CURVE_IN_DAO1,ADD_CURVE_IN_DAO1,INIT_DAOSDATALIST1,
  RECEIVE_DAOS_MB2,EDIT_DEPTH_DATA2,RECEIVE_CURVES_INTO_DAOS2,RECEIVE_SELECTED_CURVES2,RETURN_INIT2,EDIT_CURVE_INFO2,UPDATE_DAO_ORDER2,DELECT_CURVE_IN_DAO2,ADD_CURVE_IN_DAO2,INIT_DAOSDATALIST2,REPLACE_NEW_CURVES2,
  SET_CURVETYPE,SET_CURVE1,SET_CURVE2,SET_COLOR,SET_CURVE,CHANGE_TAG,SET_ORIGIN_DATA,
  ADD_MB_LINE_DAO,ADD_MB_DEPTH_DAO,ADD_MB_YX_DAO,DELETE_MB_DAO,
  SET_GRID_NUMBER,SET_CHART_ZOOM,
  EDIT_MB_DAO_INFO,EDIT_DAO_DATALIST_INFO,EDIT_DAO_DATALIST_1_INFO,EDIT_DAO_DATALIST_2_INFO,
  DO_NULL,
  SET_CHINAMAP,SET_ORGID,
  INIT_TWODIMDATA,
  REPLACE_DOCDATA_NEW_CURVES
} from './action-types';
import {reqLogin,} from '../api';    

// 同步action：返回 对象
//---user---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const receiveUser = (user) => ({type: RECEIVE_USER, user});   // 接收user 的同步action
export const showErrorMsg = (errorMsg) => ({type: SHOW_ERROR_MSG, errorMsg});   // 显示错误信息
//---orgInfo-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const receiveOrgInfo = (orgInfo) => ({type: SHOW_ORG_INFO, orgInfo});   // 接收 当前用户的 orgInfo 

//---docData-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const setDocInReducer = (data) => ({type: RECEIVE_DOC, data})  // 保存当前文件
export const editDataDocHeaderInfo = (data) => ({type: EDIT_DOC_INFO, data})  // 编辑文件 的头信息
export const initdocData = (data) => ({type: INIT_DOCDATA, data})   // 初始化，清空数据
export const replace_docdata_new_curves = (data,docType) => ({type: REPLACE_DOCDATA_NEW_CURVES , data, docType})

//---mbData-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const setMbInReducer = (data) => ({type: RECEIVE_MB, data})  // 保存当前模板
export const initmbData = (data) => ({type: INIT_MBDATA, data})   // 初始化，清空数据
export const addMbLineDao = () => ({type: ADD_MB_LINE_DAO})   // 添加模板线性道
export const addMbDepthDao = () => ({type: ADD_MB_DEPTH_DAO})  // 添加模板深度道
export const addMbXyDao = () => ({type: ADD_MB_YX_DAO})   // 添加模板岩性道
export const deleteMbDao = (daoId) => ({type: DELETE_MB_DAO, daoId})   // 删除道

//---defaultmbData-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const setDefaultMbInReducer = (data) => ({type: RECEIVE_DEFAULT_MB, data})  // 保存当前模板

//---daosDataList------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getDaosMb = (daoMb) => ({ type: RECEIVE_DAOS_MB, daoMb})    // 获取道模板
export const updateDaoOrder = (data) => ({ type: UPDATE_DAO_ORDER, data})    // 更新道顺序
export const getCurvesIntoDao = (data) => ({type: RECEIVE_CURVES_INTO_DAOS, data})   // 将选中的文件中的曲线取出 ，分别放入道中
export const getEditCurveInfo = (data) => ({type: EDIT_CURVE_INFO, data})  // 修改曲线属性
export const addYxDataInfo = (data) => ({type: ADD_YX_DATA, data})  // 用户添加岩性数据信息
export const delectCurveInDao = (data) => ({type: DELECT_CURVE_IN_DAO, data})    // 将Y移除的曲线从道中删除
export const addCurveInDao = (data) => ({type: ADD_CURVE_IN_DAO, data})    // 将 移入的曲线 添加到道中
export const addLineDao = () => ({type: ADD_LINE_DAO})   // 添加线性道
export const addDepthDao = () => ({type: ADD_DEPTH_DAO})  // 添加深度道
export const addXyDao = () => ({type: ADD_YX_DAO})   // 添加岩性道
export const deleteDao = (daoId) => ({type: DELETE_DAO, daoId})   // 删除道
export const addNewCurveInDao = (data,xData,curve_id,curvesCount) => ({type: ADD_NEW_CURVE_IN_DAO, data,xData,curve_id,curvesCount})   // 将新曲线添加到道（模板）中
export const initDaosDataList = (data) => ({type: INIT_DAOSDATALIST, data})   // 初始化，清空数据
export const dragCurveData = (data) => ({type: DRAG_CURVE_DATA, data})   // 拖拽曲线，更新/重置 数据xdata
export const replaceNewCurves = (data) => ({type: REPLACE_NEW_CURVES, data})  // 处理后的新曲线更新   


//---depthDataa--------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const setEditDepthData = (min,max,interval) => ({ type: EDIT_DEPTH_DATA, data:{min,max,interval} })    // 获取 修改后的深度

//---selectedCurves----------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getSelectedCurves = (data) => ({type: RECEIVE_SELECTED_CURVES, data})    // 选中的曲线 进行处理
export const returnInit = (data) => ({type: RETURN_INIT, data})    // 选中的曲线 进行处理

//---selectedCurves----------------------------------------------------------------------------------------------------------------------------------------------------------------
export const isCancleSelected = (data) => ({type: CANCEL_SELECTED, data})    // 取消曲线的选中状态 

//---updateDraggable---------------------------------------------------------------------------------------------------------------------------------------------------------------
export const updateDraggable = (data) => ({type: UPDATE_DRAGGABLE, data})    // 是否可以人工绘制

//---videoClickYdata---------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getVideoClickYdata = (data) => ({type: SAVE_VIDEO_CLICK_YDATA, data})    // 用户点击曲线的点坐标（y坐标）：用于同步视频播放

//---videodirection---------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getVideoDirection = (data) => ({type: SAVE_VIDEO_DIRECTION, data})    // 视频正反向同步

//---videoMarkline---------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getVideoMarkline = (data) => ({type: SAVE_VIDEO_MARKLINE, data})    // 视频正反向同步
 
//---curvesCount---------------------------------------------------------------------------------------------------------------------------------------------------------
export const getCurvesCount = (data) => ({type: GET_CURVES_COUNT, data})    // 获取最大曲线数

//---updatePages--------------------------------------------------------------------------------------------------------------------------------------  
export const isUpdatePages = (data) => ({type: IS_UPDATE_PAGES, data})    // 实时更新页面



//-------------------------------------------------------------------------------------------二维、三维数据展示-----------------------------------------------------------------------------
//--------daosDataList1------------
export const getDaosMb1 = (daoMb) => ({ type: RECEIVE_DAOS_MB1, daoMb})    // 获取道模板
export const getCurvesIntoDao1 = (data) => ({type: RECEIVE_CURVES_INTO_DAOS1, data})   // 将选中的文件中的曲线取出 ，分别放入道中
export const getEditCurveInfo1 = (data) => ({type: EDIT_CURVE_INFO1, data})  // 修改曲线属性
export const updateDaoOrder1 = (data) => ({ type: UPDATE_DAO_ORDER1, data})    // 更新道顺序
export const delectCurveInDao1 = (data) => ({type: DELECT_CURVE_IN_DAO1, data})    // 将Y移除的曲线从道中删除 
export const addCurveInDao1 = (data) => ({type: ADD_CURVE_IN_DAO1, data})    // 将 移入的曲线 添加到道中
export const initDaosDataList1 = (data) => ({type: INIT_DAOSDATALIST1, data})   // 初始化，清空数据  
export const initTwoDimData = () => ({type: INIT_TWODIMDATA})   // 初始化，清空数据  

//---depthDataa1--------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const setEditDepthData1 = (min,max,interval) => ({ type: EDIT_DEPTH_DATA1, data:{min,max,interval} })    // 获取 修改后的深度
//---selectedCurves----------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getSelectedCurves1 = (data) => ({type: RECEIVE_SELECTED_CURVES1, data})    // 选中的曲线 进行处理
export const returnInit1 = (data) => ({type: RETURN_INIT1, data})    // 选中的曲线 进行处理
//-------------twoDimData--------------------------
export const setCurveType=(data) => ({type: SET_CURVETYPE, data})
export const setCurve1=(data) => ({type: SET_CURVE1, data})
export const setCurve2=(data) => ({type: SET_CURVE2, data})  
export const setColor=(data) => ({type: SET_COLOR, data})
//--------------threeDimData------------------------
export const setCurve=(data) => ({type: SET_CURVE, data})
export const changeTag=(data) => ({type: CHANGE_TAG, data}) 
//-------------------------------------------------------------------------------------------数据对比-----------------------------------------------------------------------------
//--------daosDataList2------------
export const getDaosMb2 = (daoMb) => ({ type: RECEIVE_DAOS_MB2, daoMb})    // 获取道模板
export const getCurvesIntoDao2 = (data) => ({type: RECEIVE_CURVES_INTO_DAOS2, data})   // 将选中的文件中的曲线取出 ，分别放入道中
export const getEditCurveInfo2 = (data) => ({type: EDIT_CURVE_INFO2, data})  // 修改曲线属性
export const updateDaoOrder2 = (data) => ({ type: UPDATE_DAO_ORDER2, data})    // 更新道顺序
export const delectCurveInDao2 = (data) => ({type: DELECT_CURVE_IN_DAO2, data})    // 将Y移除的曲线从道中删除
export const addCurveInDao2 = (data) => ({type: ADD_CURVE_IN_DAO2, data})    // 将 移入的曲线 添加到道中
export const initDaosDataList2 = (data) => ({type: INIT_DAOSDATALIST2, data})   // 初始化，清空数据
export const replaceNewCurves2 = (data) => ({type: REPLACE_NEW_CURVES2, data})  // 处理后的新曲线更新(平移)   
//---depthDataa2--------------------------------------------------------------------------------------------------------------------------------------------------------------------
export const setEditDepthData2 = (min,max,interval) => ({ type: EDIT_DEPTH_DATA2, data:{min,max,interval} })    // 获取 修改后的深度
//---selectedCurves----------------------------------------------------------------------------------------------------------------------------------------------------------------
export const getSelectedCurves2 = (data) => ({type: RECEIVE_SELECTED_CURVES2, data})    // 选中的曲线 进行处理
export const returnInit2 = (data) => ({type: RETURN_INIT2, data})    // 选中的曲线 进行处理 
//DO_NULL
export const doNull = (data) => ({type: DO_NULL, data})    // 选中的曲线 进行处理

//------------------------------------------原始数据展示
export const setOriginData=(data,header)=>({type:SET_ORIGIN_DATA,data,header})//获取修改原始数据中的数据

//-----------------------------------------设置数据处理--栅格信息
export const setGridNumber=(data)=>({type:SET_GRID_NUMBER,data})

export const setChartZoom=(data)=>({type:SET_CHART_ZOOM,data})

//编辑道的基础信息
export const editMbDaoInfo = (data) =>({type:EDIT_MB_DAO_INFO,data})

export const editDaoDataListInfo = (data) =>({type:EDIT_DAO_DATALIST_INFO,data})
export const editDaoDataList1Info = (data) =>({type:EDIT_DAO_DATALIST_1_INFO,data})
export const editDaoDataList2Info = (data) =>({type:EDIT_DAO_DATALIST_2_INFO,data})

export const setChinaMap = (data) => ({type:SET_CHINAMAP,data})
export const setOrgId = (data) => ({type:SET_ORGID,data})











// 异步action：返回 函数


//登录 的异步action
export const login = (userMobile,userPassword) => {  
  return async(dispatch) => {  // 执行异步（定时器，ajax请求，promise）
    // 1. 执行 登录 的异步ajax请求
    const result = await reqLogin(userMobile,userPassword);   
    console.log("登录",result)
    if(result.code === 200){
      // console.log("登录成功")
      // 2.1. 如果成功，分发成功的同步action 
      const user = result.data.user;
      const token = result.data.token;
      setUser(user);  // 将user存入localStorage中
      setToken(token);
      dispatch(receiveUser(user));  
      // 存入 权限按钮
      // console.log("存入按钮")
      var userButtons =[];
      var orgButtons =[];   
      var wsButtons =[];
      var roleButtons =[];
      var dataMgtButtons =[];
      var dataProcessButtons =[];
      var dataShowButtons =[];
      var homeButtons =[];
      user.user_roles.map((item1) => {
        item1.role_menus.map((item2) => {
          if(item2.key === '/user-mgt'){
            item2.buttons.map((item3) => {
              userButtons.push(item3.value);
            })
          }else if(item2.key === '/orgzanition-mgt'){
            item2.buttons.map((item3) => {
              orgButtons.push(item3.value);
            })
          }else if(item2.key === '/workshop-mgt'){
            item2.buttons.map((item3) => {
              wsButtons.push(item3.value);
            })
          }else if(item2.key === '/roles-mgt'){
            item2.buttons.map((item3) => {
              roleButtons.push(item3.value);
            })
          }else if(item2.key === '/data-mgt'){
            item2.buttons.map((item3) => {
              dataMgtButtons.push(item3.value);
            })
          }else if(item2.key === '/data-process'){
            item2.buttons.map((item3) => {
              dataProcessButtons.push(item3.value);
            })
          }else if(item2.key === '/data-show'){
            item2.buttons.map((item3) => {
              dataShowButtons.push(item3.value);
            })
          }
          else if(item2.key === '/home'){
            item2.buttons.map((item3) => {
              homeButtons.push(item3.value);
            })
          }
        })
      })
      var rolesButtons = [
        {
          key: '/user-mgt',
          buttons: userButtons
        },{
          key: '/orgzanition-mgt',
          buttons: orgButtons
        },{
          key: '/workshop-mgt',
          buttons: wsButtons
        },{
          key: '/roles-mgt',
          buttons: roleButtons
        },{
          key: '/data-mgt',
          buttons: dataMgtButtons
        },{
          key: '/data-process',
          buttons: dataProcessButtons
        },{
          key: '/data-show',
          buttons: dataShowButtons
        },{
          key: '/home',
          buttons: homeButtons
        }
      ]
      // console.log("存入按钮111",roleButtons)
      setRolesButtons(rolesButtons);  // 将rolesButtons存入localStorage中
      let tmp={}
      rolesButtons.map(item=>{
        if(item.key=='/home'){
          if(item.buttons.length==0){
            tmp.state=false;
          }else{
            item.buttons.map(el=>{
              if(el=='get-chinamap-info'){
                tmp.state=true;
              }
            })
          }
        }
      })
      tmp.org_id=user.org?user.org.org_id:null
      return tmp;
    }else{
      // 2.2. 如果失败
      message.error(result.data);
    }    
  }
};  

// 退出登录 
export const logout = () => {  
  removeUser();
  removeOrgInfo();
  deleteToken();
  window.localStorage.removeItem('hasChinaMap')
  window.localStorage.removeItem("roles_buttons")
  window.localStorage.removeItem("organizationId")
  window.localStorage.removeItem("org_id")
  return {type: RESET_USER}
};   


// 当前用户的组织信息 
// export const orgInfo = (org_id,org_pid) => {  
//   return async(dispatch) => {  
//     const result = await reqOrgInfoByOrgid(org_id,org_pid);  
//     console.log("lalal",result) 
//     if(result.code === 200){
//       const orgInfo = result.data;   
//       setOrgInfo(orgInfo);  // 将orgInfo存入localStorage中
//       dispatch(receiveOrgInfo(orgInfo));  
//     }else{
//       message.error(result.data);        //   获取当前组织  请求失败！！！！！！！！！！！找原因进行处理
//     }    
//   }
// }; 




 

 
 